package com.shaoshaohuo.app.ui.ec;

import java.util.ArrayList;
import java.util.List;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.constant.ListViewAction;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.PurchaseOrder;
import com.shaoshaohuo.app.entity.PurchaseOrderEntity;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.adapter.PurchaseOrderListAdapter;
import com.shaoshaohuo.app.view.TopbarView;
import com.shaoshaohuo.app.view.XListView;
import com.shaoshaohuo.app.view.XListView.IXListViewListener;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;

/**
 * @author: yusonglin
 * @类 说 明:
 * @version 1.0
 * @创建时间：2015年8月31日 上午10:19:42
 * 
 */
public class PurchaseOrderActivity extends BaseActivity implements OnClickListener, IXListViewListener {

	/**
	 *从支付结果，打开这个activity时，传入此参
	 */
	public static final String KEY_ACTIVITY_FROM = "key_from";
	public static final String KEY_ACTIVITY_FROM_PAY = "from_pay";
	
    public static final int STATUS_DAIFUKUAN = 1;
    public static final int STATUS_DAIFAHUO = 2;
    public static final int STATUS_YISHOUHUO = 3;
    public static final int STATUS_FINISH = 4;

    private TopbarView mTopbarView;

    private TextView mDaifukuanText;
    private TextView mDaifahuoText;
    private TextView mYifahuoText;
    private TextView mYiwanchengText;

    private XListView mListView;

    private PurchaseOrderListAdapter<PurchaseOrder> mAdapter;

    private List<PurchaseOrder> mDataList = new ArrayList<PurchaseOrder>();
    private int offset = 10;
    private String action = ListViewAction.NEW;
    private String cursor = "0";
    private int status = STATUS_DAIFUKUAN;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(new TopbarView(this));
        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                setContentView(R.layout.activity_purchase_order);
                initView();
                setUpView();
            }
        }, 200);
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

        mDaifukuanText = (TextView) this.findViewById(R.id.textview_daifukuan);
        mDaifahuoText = (TextView) this.findViewById(R.id.textview_daifahuo);
        mYifahuoText = (TextView) this.findViewById(R.id.textview_yifahuo);
        mYiwanchengText = (TextView) this.findViewById(R.id.textview_yiwancheng);

        mListView = (XListView) this.findViewById(R.id.listview_data_list);

    }

    private void setUpView() {
        mTopbarView.setCenterText("采购订单");
        mTopbarView.setLeftView(true, true);

        mDaifukuanText.setOnClickListener(this);
        mDaifahuoText.setOnClickListener(this);
        mYifahuoText.setOnClickListener(this);
        mYiwanchengText.setOnClickListener(this);

        String keyFrom = getIntent().getStringExtra(KEY_ACTIVITY_FROM);
        if (keyFrom != null && KEY_ACTIVITY_FROM_PAY.equals(keyFrom)) {
        	onClick(mDaifahuoText);
        } else {
        	onClick(mDaifukuanText);
        }
        

        // List<String> list = new ArrayList<>();
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        // list.add("");
        //
        // PurchaseOrderListAdapter<String> adapter = new PurchaseOrderListAdapter<>(this, list,
        // false);
        // mListView.setAdapter(adapter);

        // mListView.setOnItemClickListener(new OnItemClickListener() {
        //
        // @Override
        // public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        // Intent intent = new Intent(PurchaseOrderActivity.this,PurchaseOrderDetailActivity.class);
        // startActivity(intent);
        // }
        // });

        setListView();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.textview_daifukuan:
            if (mDaifukuanText.isSelected()) {
                return;
            }
            mDaifukuanText.setSelected(true);
            mDaifahuoText.setSelected(false);
            mYifahuoText.setSelected(false);
            mYiwanchengText.setSelected(false);
            changeStatus(STATUS_DAIFUKUAN);
            break;
        case R.id.textview_daifahuo:
            if (mDaifahuoText.isSelected()) {
                return;
            }
            mDaifukuanText.setSelected(false);
            mDaifahuoText.setSelected(true);
            mYifahuoText.setSelected(false);
            mYiwanchengText.setSelected(false);
            changeStatus(STATUS_DAIFAHUO);
            break;
        case R.id.textview_yifahuo:
            if (mYifahuoText.isSelected()) {
                return;
            }
            mDaifukuanText.setSelected(false);
            mDaifahuoText.setSelected(false);
            mYifahuoText.setSelected(true);
            mYiwanchengText.setSelected(false);
            changeStatus(STATUS_YISHOUHUO);
            break;
        case R.id.textview_yiwancheng:
            if (mYiwanchengText.isSelected()) {
                return;
            }
            mDaifukuanText.setSelected(false);
            mDaifahuoText.setSelected(false);
            mYifahuoText.setSelected(false);
            mYiwanchengText.setSelected(true);
            changeStatus(STATUS_FINISH);
            break;
        }
    }

    private void getOrderList(final int state,boolean isLoading) {
    	
        if (isLoading) {
            startLoadingDialog();
        }
        RequestService.getInstance().getPurchaseOrderList(this, status + "", cursor, offset + "", action, PurchaseOrderEntity.class,
                        new RequestListener() {

                            @Override
                            public void onSuccess(int requestCode, BaseEntity resultData) {
                                dismissLoadingDialog();
                                if (resultData.isOk()) {
                                	if(state == status){
                                		PurchaseOrderEntity entity = (PurchaseOrderEntity) resultData;
                                		List<PurchaseOrder> list = entity.getData().getList();
                                		mDataList.addAll(list);
                                		
                                		if ((mDataList.size() < offset) || (action.equals(ListViewAction.HISTORY) && list.size() < offset)) {
                                			// mListView.setPullLoadEnable(false);
                                			mListView.endLoadingMore("没有更多了");
                                		}
                                		setAdapter(mDataList);
                                	}

                                } else {
                                    showToast(resultData.getMsg());
                                }
                                onLoad();
                            }

                            @Override
                            public void onFailed(int requestCode, Exception error, String msg) {
                                dismissLoadingDialog();
                                showToast(msg);
                                onLoad();
                            }
                        });
    }

    protected void setAdapter(List<PurchaseOrder> mDataList2) {
        if (mAdapter == null) {
            mAdapter = new PurchaseOrderListAdapter<PurchaseOrder>(this, mDataList2, false);
            mListView.setAdapter(mAdapter);
        } else {
            mAdapter.setData(mDataList2);
        }
    }

    private void setListView() {
        mListView.setPullLoadEnable(true);
        mListView.setPullRefreshEnable(true);
        mListView.setXListViewListener(this);

        mListView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                PurchaseOrder order = (PurchaseOrder) parent.getItemAtPosition(position);
                if("0".equals(order.getStatus())){
                    Intent intent = new Intent(PurchaseOrderActivity.this,PurchaseRecordDetailActivity.class);
                    intent.putExtra(ExtraName.id, order.getOrderid());
                    startActivity(intent);
                }else{
                    Intent intent = new Intent(PurchaseOrderActivity.this, PurchaseOrderDetailActivity.class);
                    intent.putExtra(ExtraName.orderid, order.getOrderid());
                    startActivity(intent);
                }
                
            }
        });
    }

    private void changeStatus(int s) {
        startLoadingDialog();
        mDataList.clear();
        status = s;
        onRefresh();
    }

    @Override
    public void onRefresh() {
//        if (mDataList.size() > 0) {
//            cursor = mDataList.get(0).getCursor();
//        } else {
            cursor = "0";
//        }
            mDataList.clear();
        action = ListViewAction.NEW;
        mListView.setPullLoadEnable(true);
        getOrderList(status,false);
    }

    @Override
    public void onLoadMore() {
        if (mDataList.size() > 0) {
            cursor = mDataList.get(mDataList.size() - 1).getCursor();
        } else {
            cursor = "";
        }
        action = ListViewAction.HISTORY;
        getOrderList(status,false);
    }

    private void onLoad() {
        mListView.stopRefresh();
        mListView.stopLoadMore();
        // mListView.setRefreshTime(StringHelper.formatDate(System.currentTimeMillis()+""));
    }

}
